import Vue from 'vue'
import Router from 'vue-router'
const _import = require('./_import_' + process.env.NODE_ENV);
import Layout from '../webedi/layout/Layout';

const Temp = _import('Template');
const Login = _import('Login');
const dashboard = _import('dashboard/index');
const Err404 = _import('404');
const UserEnt = _import('user/UserEnt');
const UserInq = _import('user/UserInq');
const UserPassword = _import('user/UserPassword');
const ResetPassword = _import('user/ResetPassword');
const ApprDocEnt = _import('apprDoc/ApprDocEnt');
const ApprDocInq = _import('apprDoc/ApprDocInq');
const ApprDocDetail = _import('apprDoc/ApprDocDetail');
const ApprDocUpd = _import('apprDoc/ApprDocUpd');
const ApprDocConfirm = _import('apprDoc/ApprDocConfirm');
const ApprDocDownLoad = _import('apprDoc/ApprDocDownLoad');
const EstEntInq = _import('est/EstEntInq');
const EstEnt = _import('est/EstEnt');
const EstInq = _import('est/EstInq');
const EstDetail = _import('est/EstDetail');
const EstUpd = _import('est/EstUpd');
const OrderEntInq = _import('order/OrderEntInq');
const OrderEnt = _import('order/OrderEnt');
const OrderInq = _import('order/OrderInq');
const OrderDetail = _import('order/OrderDetail');
const OrderUpd = _import('order/OrderUpd');
const OrderConfirm = _import('order/OrderConfirm');
const MonReqInq = _import('salesReq/MonReqInq');

const  RoleEnt = _import('role/RoleEnt');
const  RoleInq = _import('role/RoleInq');
const  RoleDetail = _import('role/RoleDetail');
const  RoleUpd = _import('role/RoleUpd');


Vue.use(Router);


const componentsMap = {
  "Layout"          : Layout,
  "Temp"            : Temp,                //開発中テンプレート
  "Login"           : Login,               //ログイン
  "dashboard"       : dashboard,           //ホームページ
  "UserEnt"         : UserEnt,             //ユーザー登録
  "UserInq"         : UserInq,             //ユーザー検索
  "UserPassword"    : UserPassword,        //パスワード変更
  "ResetPassword"   : ResetPassword,       //パスワードリセット
  "ApprDocEnt"      : ApprDocEnt,          //稟議書登録
  "ApprDocInq"      : ApprDocInq,          //稟議書検索
  "ApprDocDetail"   : ApprDocDetail,       //稟議書詳細
  "ApprDocUpd"      : ApprDocUpd,          //稟議書更新
  "ApprDocConfirm"  : ApprDocConfirm,      //稟議書確認
  "ApprDocDownLoad" : ApprDocDownLoad,     //月次処理用の稟議書データダウンロード
  "EstEntInq"       : EstEntInq,           //見積検索登録
  "EstEnt"          : EstEnt,              //見積登録
  "EstInq"          : EstInq,              //見積検索
  "EstDetail"       : EstDetail,           //見積詳細
  "EstUpd"          : EstUpd,              //見積更新
  "OrderEntInq"     : OrderEntInq,         //受注検索登録
  "OrderEnt"        : OrderEnt,            //受注登録
  "OrderInq"        : OrderInq,            //受注検索
  "OrderDetail"     : OrderDetail,         //受注詳細
  "OrderUpd"        : OrderUpd,            //受注更新
  "OrderConfirm"    : OrderConfirm,        //受注確定
  "MonReqInq"       : MonReqInq,           //月次請求一覧

  "RoleEnt"         : RoleEnt,             //ロール登録
  "RoleInq"         : RoleInq,             //ロール検索
  "RoleDetail"      : RoleDetail,          //ロール詳細
  "RoleUpd"         : RoleUpd,             //ロール更新

  "Err404"          : Err404              //エラー画面
};

export const constantRouterMap = [
  { path: '/login', component: componentsMap.Login, hidden: true, meta:{keepAlive: false}},
  { path: '/404', component: componentsMap.Err404, hidden: true },
  {
    path: '/',
    component: componentsMap.Layout,
    redirect: '/dashboard',
    name: 'Home',
    hidden: true,
    meta:{keepAlive: false},
    children: [{ path: 'dashboard', name: '掲示板', component: dashboard }]
  },
  { path: '*', redirect: '/404', hidden: true }
];

export default new Router({
  // mode: 'history', //后端支持可开
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
});

//Compenentの名称から対象へ変換処理
export function convertMenu (x_menu_list = [], routes = [], level = 1){
    for (let i = 0, len = x_menu_list.length; i < len; i++){
      let item = x_menu_list[i];
      if(typeof item.component !== "undefined" ){
          if(typeof componentsMap[item.component] !== "undefined"){
              item.component = componentsMap[item.component];
          }else{
            item.component = Temp;
          }
      };

      if(typeof item.children !== "undefined" ){
        convertMenu(item.children,routes, level+1);
      };

      if(level ===1) routes.push(item);

    }//end for

    return routes;
}




